package com.ymatou.iapi.userservice.testcase;

import java.util.List;
import java.util.Map;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.userservice.parameter.enums.AppSourceEnum;
import com.ymatou.iapi.userservice.parameter.enums.ChannelSourceEnum;
import com.ymatou.iapi.userservice.parameter.enums.TerminalSourceEnum;
import com.ymatou.iapi.userservice.parameter.enums.ThirdPartyTypeEnum;
import com.ymatou.iapi.userservice.parameter.enums.UserTypeEnum;
import com.ymatou.iapi.userservice.parameter.enums.VisitLanguageEnum;
import com.ymatou.iapi.userservice.parameter.req.BindThirdPartyUserReq;
import com.ymatou.iapi.userservice.parameter.req.CreateUserReq;
import com.ymatou.iapi.userservice.parameter.req.ThirdPartyUserReq;
import com.ymatou.iapi.userservice.service.BindThirdPartyUserCall;
import com.ymatou.iapi.userservice.service.CreateUserCall;
import com.ymatou.mq.admin.parameter.MessageListBean;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.JUserCallService;
import com.ymttest.business.service.MqQueryCallService;
import com.ymttest.database.model.ThirdpartyUserbind;
import com.ymttest.database.model.ThirdpartyUserbindHistory;
import com.ymttest.database.model.Userinfo;
import com.ymttest.database.model.Userinfoex;
import com.ymttest.database.sqlwapper.JUserWapper;
import com.ymttest.utils.TestDataManager;
/**
*锁定用户接口
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_BindThirdPartyUser{
	private static BindThirdPartyUserReq BindThirdPartyUserReq;
	private static BindThirdPartyUserCall BindThirdPartyUserCall;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("绑定第三方用户接口");
	}

	@Before
	public void caseUp() {
		BindThirdPartyUserReq=new BindThirdPartyUserReq();
		BindThirdPartyUserCall=new BindThirdPartyUserCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(BindThirdPartyUserCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_BindThirdPartyUser_001() {
		Logger.start(true,"绑定weixin新用户成功");
		try {
			CreateUserReq cur=new CreateUserReq();
			cur=JUserCallService.initCreateUserBean(cur);
			int uid = JUserCallService.createUserCall(cur);
			BindThirdPartyUserReq=JUserCallService.initBindThirdUserReq(uid);
			BindThirdPartyUserReq.setThirdPartyType(ThirdPartyTypeEnum.WeiXin);
			BindThirdPartyUserCall.setData(BindThirdPartyUserReq);
			BindThirdPartyUserCall.callService();
			Logger.verifyEquals("true", BindThirdPartyUserCall.getString("Success"), "验证Success");
			Logger.verifyEquals("200", BindThirdPartyUserCall.getString("ResponseCode"), "验证ResponseCode");
			verifyDB(uid,BindThirdPartyUserReq);
			MessageListBean MessageListBean = MqQueryCallService.initCreateWx(uid);
			List<Map> joa = MqQueryCallService.GetMqMessage(MessageListBean);
			if (joa.size() == 0) {
				Thread.sleep(5000);
				joa = MqQueryCallService.GetMqMessage(MessageListBean);
			}
			joa.forEach(ele -> {
				Logger.debug(ele.toString());
			});
			Logger.verifyEquals(true, joa.size() > 0, "验证发送了总线消息");
			//body:{"openId":"ffffffffffb866be","unionId":"ffffffffffb866be","userId":24682518}
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category(P2.class)
	@TestCase
	public void Tc_BindThirdPartyUser_002() {
		Logger.start(true,"绑定weibo新用户成功");
		try {
			CreateUserReq cur=new CreateUserReq();
			cur=JUserCallService.initCreateUserBean(cur);
			int uid = JUserCallService.createUserCall(cur);
			BindThirdPartyUserReq=JUserCallService.initBindThirdUserReq(uid);
			BindThirdPartyUserReq.setThirdPartyType(ThirdPartyTypeEnum.Weibo);
			BindThirdPartyUserCall.setData(BindThirdPartyUserReq);
			BindThirdPartyUserCall.callService();
			Logger.verifyEquals("true", BindThirdPartyUserCall.getString("Success"), "验证Success");
			Logger.verifyEquals("200", BindThirdPartyUserCall.getString("ResponseCode"), "验证ResponseCode");
			verifyDB(uid,BindThirdPartyUserReq);
			MessageListBean MessageListBean = MqQueryCallService.initCreateWx(uid);
			List<Map> joa = MqQueryCallService.GetMqMessage(MessageListBean);
			if (joa.size() == 0) {
				Thread.sleep(5000);
				joa = MqQueryCallService.GetMqMessage(MessageListBean);
			}
			joa.forEach(ele -> {
				Logger.debug(ele.toString());
			});
			Logger.verifyEquals(true, joa.size() == 0, "验证不会发送了总线消息");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category(P2.class)
	@TestCase
	public void Tc_BindThirdPartyUser_003() {
		Logger.start(true,"绑定Alipay新用户成功");
		try {
			CreateUserReq cur=new CreateUserReq();
			cur=JUserCallService.initCreateUserBean(cur);
			int uid = JUserCallService.createUserCall(cur);
			BindThirdPartyUserReq=JUserCallService.initBindThirdUserReq(uid);
			BindThirdPartyUserReq.setThirdPartyType(ThirdPartyTypeEnum.Alipay);
			BindThirdPartyUserCall.setData(BindThirdPartyUserReq);
			BindThirdPartyUserCall.callService();
			Logger.verifyEquals("true", BindThirdPartyUserCall.getString("Success"), "验证Success");
			Logger.verifyEquals("200", BindThirdPartyUserCall.getString("ResponseCode"), "验证ResponseCode");
			verifyDB(uid,BindThirdPartyUserReq);
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category(P2.class)
	@TestCase
	public void Tc_BindThirdPartyUser_004() {
		Logger.start(false,"重复绑定weixin用户失败");
		try {
			CreateUserReq cur=new CreateUserReq();
			cur=JUserCallService.initCreateUserBean(cur);
			int uid = JUserCallService.createUserCall(cur);
			BindThirdPartyUserReq=JUserCallService.initBindThirdUserReq(uid);
			BindThirdPartyUserReq.setThirdPartyType(ThirdPartyTypeEnum.WeiXin);
			BindThirdPartyUserCall.setData(BindThirdPartyUserReq);
			BindThirdPartyUserCall.callService();
			BindThirdPartyUserCall.callService();
			Logger.verifyEquals("false", BindThirdPartyUserCall.getString("Success"), "验证Success");
			Logger.verifyEquals("100015", BindThirdPartyUserCall.getString("ResponseCode"), "验证ResponseCode");
			Logger.verifyIsContains("bindThirdPartyUser error", BindThirdPartyUserCall.getString("LastErrorMessage"), "验证LastErrorMessage");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category(P2.class)
	@TestCase
	public void Tc_BindThirdPartyUser_005() {
		Logger.start(false,"重复绑定Weibo用户失败");
		try {
			CreateUserReq cur=new CreateUserReq();
			cur=JUserCallService.initCreateUserBean(cur);
			int uid = JUserCallService.createUserCall(cur);
			BindThirdPartyUserReq=JUserCallService.initBindThirdUserReq(uid);
			BindThirdPartyUserReq.setThirdPartyType(ThirdPartyTypeEnum.Weibo);
			BindThirdPartyUserCall.setData(BindThirdPartyUserReq);
			BindThirdPartyUserCall.callService();
			BindThirdPartyUserCall.callService();
			Logger.verifyEquals("false", BindThirdPartyUserCall.getString("Success"), "验证Success");
			Logger.verifyEquals("100015", BindThirdPartyUserCall.getString("ResponseCode"), "验证ResponseCode");
			Logger.verifyIsContains("bindThirdPartyUser error", BindThirdPartyUserCall.getString("LastErrorMessage"), "验证LastErrorMessage");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category(P2.class)
	@TestCase
	public void Tc_BindThirdPartyUser_006() {
		Logger.start(false,"重复绑定Alipay用户失败");
		try {
			CreateUserReq cur=new CreateUserReq();
			cur=JUserCallService.initCreateUserBean(cur);
			int uid = JUserCallService.createUserCall(cur);
			BindThirdPartyUserReq=JUserCallService.initBindThirdUserReq(uid);
			BindThirdPartyUserReq.setThirdPartyType(ThirdPartyTypeEnum.Alipay);
			BindThirdPartyUserCall.setData(BindThirdPartyUserReq);
			BindThirdPartyUserCall.callService();
			BindThirdPartyUserCall.callService();
			Logger.verifyEquals("false", BindThirdPartyUserCall.getString("Success"), "验证Success");
			Logger.verifyEquals("100015", BindThirdPartyUserCall.getString("ResponseCode"), "验证ResponseCode");
			Logger.verifyIsContains("bindThirdPartyUser error", BindThirdPartyUserCall.getString("LastErrorMessage"), "验证LastErrorMessage");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category(P2.class)
	@TestCase
	public void Tc_BindThirdPartyUser_007() {
		Logger.start(false,"不同用户重复绑定weixin用户失败");
		try {
			CreateUserReq cur=new CreateUserReq();
			cur=JUserCallService.initCreateUserBean(cur);
			int uid = JUserCallService.createUserCall(cur);
			
			CreateUserReq cur2=new CreateUserReq();
			cur2=JUserCallService.initCreateUserBean(cur2);
			int uid2 = JUserCallService.createUserCall(cur2);
			
			
			BindThirdPartyUserReq=JUserCallService.initBindThirdUserReq(uid);
			BindThirdPartyUserReq.setThirdPartyType(ThirdPartyTypeEnum.WeiXin);
			BindThirdPartyUserCall.setData(BindThirdPartyUserReq);
			BindThirdPartyUserCall.callService();
			
			BindThirdPartyUserReq.setUserId(uid2);
			BindThirdPartyUserCall.callService();
			
			Logger.verifyEquals("false", BindThirdPartyUserCall.getString("Success"), "验证Success");
			Logger.verifyEquals("100015", BindThirdPartyUserCall.getString("ResponseCode"), "验证ResponseCode");
			Logger.verifyIsContains("bindThirdPartyUser error", BindThirdPartyUserCall.getString("LastErrorMessage"), "验证LastErrorMessage");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category(P2.class)
	@TestCase
	public void Tc_BindThirdPartyUser_008() {
		Logger.start(true,"绑定qq新用户成功");
		try {
			CreateUserReq cur=new CreateUserReq();
			cur=JUserCallService.initCreateUserBean(cur);
			int uid = JUserCallService.createUserCall(cur);
			BindThirdPartyUserReq=JUserCallService.initBindThirdUserReq(uid);
			BindThirdPartyUserReq.setThirdPartyType(ThirdPartyTypeEnum.QQ);
			BindThirdPartyUserCall.setData(BindThirdPartyUserReq);
			BindThirdPartyUserCall.callService();
			Logger.verifyEquals("true", BindThirdPartyUserCall.getString("Success"), "验证Success");
			Logger.verifyEquals("200", BindThirdPartyUserCall.getString("ResponseCode"), "验证ResponseCode");
			verifyDB(uid,BindThirdPartyUserReq);
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category(P2.class)
	@TestCase
	public void Tc_BindThirdPartyUser_009() {
		Logger.start(false,"不同用户重复绑定qq用户失败");
		try {
			CreateUserReq cur=new CreateUserReq();
			cur=JUserCallService.initCreateUserBean(cur);
			int uid = JUserCallService.createUserCall(cur);
			
			CreateUserReq cur2=new CreateUserReq();
			cur2=JUserCallService.initCreateUserBean(cur2);
			int uid2 = JUserCallService.createUserCall(cur2);
			
			
			BindThirdPartyUserReq=JUserCallService.initBindThirdUserReq(uid);
			BindThirdPartyUserReq.setThirdPartyType(ThirdPartyTypeEnum.QQ);
			BindThirdPartyUserCall.setData(BindThirdPartyUserReq);
			BindThirdPartyUserCall.callService();
			
			BindThirdPartyUserReq.setUserId(uid2);
			BindThirdPartyUserCall.callService();
			
			Logger.verifyEquals("false", BindThirdPartyUserCall.getString("Success"), "验证Success");
			Logger.verifyEquals("100015", BindThirdPartyUserCall.getString("ResponseCode"), "验证ResponseCode");
			Logger.verifyIsContains("bindThirdPartyUser error", BindThirdPartyUserCall.getString("LastErrorMessage"), "验证LastErrorMessage");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_BindThirdPartyUser_010() {
		Logger.start(true,"绑定weixin新用户成功,用户名特殊字符");
		try {
			CreateUserReq cur=new CreateUserReq();
			cur=JUserCallService.initCreateUserBean(cur);
			int uid = JUserCallService.createUserCall(cur);
			BindThirdPartyUserReq=JUserCallService.initBindThirdUserReq(uid);
			BindThirdPartyUserReq.setThirdPartyType(ThirdPartyTypeEnum.WeiXin);
			BindThirdPartyUserReq.setThirdPartyUserName("云云🔥1431");
			BindThirdPartyUserCall.setData(BindThirdPartyUserReq);
			BindThirdPartyUserCall.callService();
			Logger.verifyEquals("true", BindThirdPartyUserCall.getString("Success"), "验证Success");
			Logger.verifyEquals("200", BindThirdPartyUserCall.getString("ResponseCode"), "验证ResponseCode");
			verifyDB(uid,BindThirdPartyUserReq);
			MessageListBean MessageListBean = MqQueryCallService.initCreateWx(uid);
			List<Map> joa = MqQueryCallService.GetMqMessage(MessageListBean);
			if (joa.size() == 0) {
				Thread.sleep(5000);
				joa = MqQueryCallService.GetMqMessage(MessageListBean);
			}
			joa.forEach(ele -> {
				Logger.debug(ele.toString());
			});
			Logger.verifyEquals(true, joa.size() > 0, "验证发送了总线消息");
			//body:{"openId":"ffffffffffb866be","unionId":"ffffffffffb866be","userId":24682518}
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	private void verifyDB(Integer uid, ThirdPartyUserReq cq) {
		JUserWapper ju=new JUserWapper();
		
		Logger.comment("验证thirdparty_userbind");
		ThirdpartyUserbind thirdinfo = ju.selectthirdpartyuserbind(uid).get(0);
		Logger.verifyEquals(cq.getThirdPartyId(), thirdinfo.getThirdPartyId(), "验证ThirdPartyId");
		Logger.verifyEquals(cq.getThirdPartyType().getCode(), thirdinfo.getThirdPartyType(), "验证ThirdPartyType");
		Logger.verifyEquals(cq.getOpenId(), thirdinfo.getOpenId(), "验证OpenId");
		
		Logger.comment("验证thirdparty_userbind_history");
		ThirdpartyUserbindHistory thirdinfohist = ju.selectthirdpartyuserbindhistory(uid).get(0);
		Logger.verifyEquals(true, thirdinfohist.getBinding(), "验证binding状态");
		Logger.verifyEquals(cq.getCity(), thirdinfohist.getCity(), "验证city");
		Logger.verifyEquals(cq.getCountry(), thirdinfohist.getCountry(), "验证Country");
		Logger.verifyEquals(cq.getProvince(), thirdinfohist.getProvince(), "验证Province");
		Logger.verifyEquals(cq.getAppSource().getCode(), thirdinfohist.getAppSource(), "验证AppSource");
		Logger.verifyEquals(cq.getClientIp(), thirdinfohist.getClientIp(), "验证ClientIp");
		Logger.verifyEquals(cq.getDeviceId(), thirdinfohist.getDeviceId(), "验证DeviceId");
		Logger.verifyEquals(cq.getMail(), thirdinfohist.getEmail(), "验证Email");
		Logger.verifyEquals(cq.getEquimentid(), thirdinfohist.getEquimentid(), "验证Equimentid");
		Logger.verifyEquals(cq.getGmtDecay(), thirdinfohist.getGmtDecay(), "验证GmtDecay");
		Logger.verifyEquals(cq.getHeadimgurl(), thirdinfohist.getHeadimgurl(), "验证Headimgurl");
		Logger.verifyEquals(cq.getMobile(), thirdinfohist.getLoginMobile(), "验证LoginMobile");
		Logger.verifyEquals(cq.getOpenId(), thirdinfohist.getOpenId(), "验证OpenId");
		Logger.verifyEquals(cq.getSecret(), thirdinfohist.getSecret(), "验证Secret");
		Logger.verifyEquals(cq.getTerminalSource().getCode(), thirdinfohist.getTerminalSource(), "验证TerminalSource");
		Logger.verifyEquals(cq.getThirdParty(), thirdinfohist.getThirdParty(), "验证ThirdParty");
		Logger.verifyEquals(cq.getThirdPartyId(), thirdinfohist.getThirdPartyId(), "验证ThirdPartyId");
		Logger.verifyEquals(cq.getThirdPartyToken(), thirdinfohist.getThirdPartyToken(), "验证ThirdPartyToken");
		Logger.verifyEquals(cq.getThirdPartyType().getCode(), thirdinfohist.getThirdPartyType(), "验证ThirdPartyType");
		Logger.verifyEquals(cq.getThirdPartyUserName(), thirdinfohist.getThirdPartyUserName(), "验证getThirdPartyUserName");
		Logger.verifyEquals(cq.getUserGender().getCode(), thirdinfohist.getUserGender(), "验证UserGender");
		Logger.verifyEquals(cq.getGrade(), thirdinfohist.getUserGrade(), "验证UserGrade");
		Logger.verifyEquals(cq.getGradeType(), thirdinfohist.getUserGradeType(), "验证UserGradeType");
	}
}